package cn.iocoder.yudao.module.of.service.meetingminutesenduser;

import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.of.controller.admin.meetingminutesenduser.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.meetingminutesenduser.MeetingMinuteSendUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.of.dal.mysql.meetingminutesenduser.MeetingMinuteSendUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.of.enums.ErrorCodeConstants.*;

/**
 * 会议纪要下派人员 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
@Log4j2
public class MeetingMinuteSendUserServiceImpl implements MeetingMinuteSendUserService {

    @Resource
    private MeetingMinuteSendUserMapper meetingMinuteSendUserMapper;

    @Override
    public Long createMeetingMinuteSendUser(MeetingMinuteSendUserSaveReqVO createReqVO) {
        // 插入
        List<MeetingMinuteSendUserSaveReqVO> sendUserList = createReqVO.getSendUserList();
        List<MeetingMinuteSendUserDO> result = new ArrayList<>();
        for(MeetingMinuteSendUserSaveReqVO saveReqVO:sendUserList){
            createReqVO.setId(null);
            MeetingMinuteSendUserDO user = BeanUtils.toBean(createReqVO, MeetingMinuteSendUserDO.class);
            String sendUserId = saveReqVO.getSendUserId();
            user.setSendUserId(sendUserId);
            if(sendUserId != null && sendUserId.contains("-")){
                String s = sendUserId.split("-")[1];
                if(StringUtils.isNotBlank(s)){
                    user.setUserId(Long.valueOf(s));
                }
            }
            user.setSendUserName(saveReqVO.getSendUserName());
            user.setDeleted(Boolean.FALSE);
            result.add(user);
        }

         meetingMinuteSendUserMapper.insert(result);
        // 返回
        return 1L;
    }

    @Override
    public void updateMeetingMinuteSendUser(MeetingMinuteSendUserSaveReqVO updateReqVO) {

        // 校验存在
        //validateMeetingMinuteSendUserExists(updateReqVO.getMinuteId());
        // 更新
        if(updateReqVO.getMinuteId() != null) {
            meetingMinuteSendUserMapper.delete(MeetingMinuteSendUserDO::getMinuteId, updateReqVO.getMinuteId());
        }
        //meetingMinuteSendUserMapper.updateById(updateObj);
        if(updateReqVO.getSendUserList() != null && !updateReqVO.getSendUserList().isEmpty()) {
            createMeetingMinuteSendUser(updateReqVO);
        }
    }

    @Override
    public void deleteMeetingMinuteSendUser(Long id) {
        // 校验存在
        validateMeetingMinuteSendUserExists(id);
        // 删除
        meetingMinuteSendUserMapper.deleteById(id);
    }

    private void validateMeetingMinuteSendUserExists(Long id) {
        if (meetingMinuteSendUserMapper.selectById(id) == null) {
            throw exception(MEETING_MINUTES_NOT_EXISTS);
        }
    }

    @Override
    public MeetingMinuteSendUserDO getMeetingMinuteSendUser(Long id) {
        return meetingMinuteSendUserMapper.selectById(id);
    }

    @Override
    public PageResult<MeetingMinuteSendUserDO> getMeetingMinuteSendUserPage(MeetingMinuteSendUserPageReqVO pageReqVO) {
        return meetingMinuteSendUserMapper.selectPage(pageReqVO);
    }

}